﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DoAn_BumTruong_DesignPattern.Admin
{
    public partial class EditModule : System.Web.UI.Page
    {
        ModuleController mdController = new ModuleController();
        Modules module = new Modules();
        Facades facadeModule = new Facades();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btClose.Click += btClose_Click;
            btReset.Click += btReset_Click;
            btSave.Click += btSave_Click;
        }

        void btSave_Click(object sender, EventArgs e)
        {
            bool status = false;
            if (Page.IsValid)
            {
                Modules m = GetData();
                if (m.ModuleID > 0)
                {
                    status = facadeModule.UpdateModule(m);
                    if (status)
                        SetData(m);
                    lblStatus.Text = status ? "Update successfull" : "Update failed";
                }
                else
                {
                    status = facadeModule.AddModule(m);
                    lblStatus.Text = status ? "Insert successfull" : "Insert failed";
                }
            }
        }

        void btReset_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPath.Text = "";
            txtOrderNo.Text = "";
            DropDownListPosition.SelectedIndex = 0;
            CKEditorDescription.Text = "";
            CheckBoxActive.Checked = false;
        }

        void btClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Module.aspx");
        }

        //Load
        private void LoadData()
        {
            lblId.Text = Request.QueryString["ModuleID"] ?? "";
            //LoadPosition();
            if (lblId.Text != "")
            {
                lblTitle.Text = "EDIT MODULE";
                module = mdController.SingleModule(lblId.Text);
                SetData(module);
            }
            else
            {
                lblTitle.Text = "ADD NEW MODULE";
                LoadPosition();
            }
        }

        //Load Position
        private void LoadPosition()
        {
            int i = 0;
            foreach (string item in Enum.GetNames(typeof(ModulePositions)))
            {
                DropDownListPosition.Items.Add(new ListItem(item, i + ""));
                i++;
            }
        }

        //SetData
        private void SetData(Modules m)
        {
            lblId.Text = m.ModuleID.ToString();
            txtName.Text = m.Name;
            txtPath.Text = m.Path;
            CKEditorDescription.Text = m.Description;
            txtOrderNo.Text = m.OrderNo.ToString();
            LoadPosition();
            DropDownListPosition.SelectedValue = m.PositionDisplay.ToString();
            CheckBoxActive.Checked = m.Active;
        }

        //GetData
        private Modules GetData()
        {
            module = null;
            if (lblId.Text != "")
            {
                module = mdController.SingleModule(lblId.Text);
            }
            else
            {
                module = new Modules();
            }
            module.Name = txtName.Text;
            module.Path = txtPath.Text;
            module.Description = CKEditorDescription.Text;
            if (txtOrderNo.Text != "")
                module.OrderNo = int.Parse(txtOrderNo.Text);
            else
                module.OrderNo = 0;
            module.PositionDisplay = int.Parse(DropDownListPosition.SelectedValue);
            module.Active = CheckBoxActive.Checked;
            return module;
        }
    }
}